package com.icexxx.ice_browser;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

public class MyFrame extends JFrame {

    /**
     * 序列化
     */
    private static final long serialVersionUID = 2465248600806440480L;

    @Override
    protected void frameInit() {
        initData();
        super.frameInit();
        setTitle("窗口标题");
        setSize(1500, 900);
        setLocation(80, 50);
        BorderLayout borderLayout = new BorderLayout();
        setLayout(borderLayout);
        JPanel historyList = PanelManager.createHistoryList();// left
        JPanel mainPanel = PanelManager.createMainPanel();// right
        this.add(historyList, BorderLayout.WEST);
        this.add(mainPanel, BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        loadData();
        this.setVisible(true);
    }

    private void loadData() {
        String savePath = DataContext.get("savePath");
        String savePathFull = savePath + "/json.txt";
        if (!FileUtil.exist(savePathFull)) {
            // NOP
        } else {
            byte[] readBytes = FileUtil.readBytes(savePathFull);
            Object unserialize = ObjectUtil.deserialize(readBytes);
            if (unserialize instanceof List) {
                @SuppressWarnings("unchecked")
                List<Request> all = (List<Request>) unserialize;
                DataCenter.addAll(all);
                Request request = DataCenter.first();
                String url = request.getUrl();
                String param = request.getParam();
                String method = request.getMethod();
                String content = request.getContent();// TODO 代码重复
                JTextField textField = Context.getJTextField("requestAddressEditText");
                JTextArea paramTextArea = Context.getJTextArea("paramTextArea");
                JTextArea textArea = Context.getJTextArea("centerTextArea");
                JList<String> jlist = Context.getJList("list");
                JComboBox<String> requestMethodList = Context.getJComboBox("requestMethodList");
                textField.setText(url);
                int SelectedIndex = ListManager.calcSelectedIndex(method);
                requestMethodList.setSelectedIndex(SelectedIndex);
                textField.setText(url);
                paramTextArea.setText(param);
                textArea.setText(content);
                for (Request requestCur : all) {
                    String key = requestCur.getKey();
                    ListManager.addItem(jlist, key);
                }
                jlist.setSelectedIndex(0);
            }
        }

    }

    private void initData() {
        String basePath = DataContext.get("basePath");
        String savePath = DataContext.get("savePath");
        if (StrUtil.isBlank(basePath)) {
            throw new RuntimeException("basePath不能为空");
        }
        if (StrUtil.isBlank(savePath)) {
            throw new RuntimeException("savePath不能为空");
        }
        String versionPath = savePath + "/version.txt";
        VersionManager.setFileName(versionPath);
        String savePathFull = savePath + "/json.txt";
        if (!FileUtil.exist(savePathFull)) {
            // NOP
        } else {
            // NOP
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    @Override
    protected void processKeyEvent(KeyEvent e) {
        super.processKeyEvent(e);
    }

}
